package org.dromara.supplier.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.supplier.domain.bo.SupplierBo;
import org.dromara.supplier.domain.vo.SupplierVo;
import org.dromara.supplier.domain.Supplier;
import org.dromara.supplier.mapper.SupplierMapper;
import org.dromara.supplier.service.ISupplierService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 供应商Service业务层处理
 *
 * @author Lion Li
 * @date 2025-06-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SupplierServiceImpl implements ISupplierService {

    private final SupplierMapper baseMapper;

    /**
     * 查询供应商
     *
     * @param id 主键
     * @return 供应商
     */
    @Override
    public SupplierVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询供应商列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 供应商分页列表
     */
    @Override
    public TableDataInfo<SupplierVo> queryPageList(SupplierBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Supplier> lqw = buildQueryWrapper(bo);
        Page<SupplierVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的供应商列表
     *
     * @param bo 查询条件
     * @return 供应商列表
     */
    @Override
    public List<SupplierVo> queryList(SupplierBo bo) {
        LambdaQueryWrapper<Supplier> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Supplier> buildQueryWrapper(SupplierBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Supplier> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(Supplier::getId);
        lqw.like(StringUtils.isNotBlank(bo.getSupplierName()), Supplier::getSupplierName, bo.getSupplierName());
        lqw.like(StringUtils.isNotBlank(bo.getFactoryLocation()), Supplier::getFactoryLocation, bo.getFactoryLocation());
        lqw.like(StringUtils.isNotBlank(bo.getRegisteredCapital()), Supplier::getRegisteredCapital, bo.getRegisteredCapital());
        lqw.like(StringUtils.isNotBlank(bo.getMainProducts()), Supplier::getMainProducts, bo.getMainProducts());
        lqw.like(StringUtils.isNotBlank(bo.getCooperationMode()), Supplier::getCooperationMode, bo.getCooperationMode());
        lqw.like(StringUtils.isNotBlank(bo.getFanType()), Supplier::getFanType, bo.getFanType());
        lqw.like(StringUtils.isNotBlank(bo.getEnterpriseAttribute()), Supplier::getEnterpriseAttribute, bo.getEnterpriseAttribute());
        lqw.like(StringUtils.isNotBlank(bo.getContactPerson()), Supplier::getContactPerson, bo.getContactPerson());
        lqw.like(StringUtils.isNotBlank(bo.getContactPhone()), Supplier::getContactPhone, bo.getContactPhone());
        lqw.like(StringUtils.isNotBlank(bo.getResponsiblePerson()), Supplier::getResponsiblePerson, bo.getResponsiblePerson());
        lqw.like(StringUtils.isNotBlank(bo.getResponsiblePhone()), Supplier::getResponsiblePhone, bo.getResponsiblePhone());
        lqw.like(StringUtils.isNotBlank(bo.getDetailedAddress()), Supplier::getDetailedAddress, bo.getDetailedAddress());
        lqw.like(StringUtils.isNotBlank(bo.getInvoiceHead()), Supplier::getInvoiceHead, bo.getInvoiceHead());
        lqw.like(StringUtils.isNotBlank(bo.getUnitTaxNumber()), Supplier::getUnitTaxNumber, bo.getUnitTaxNumber());
        lqw.like(StringUtils.isNotBlank(bo.getLegalPersonName()), Supplier::getLegalPersonName, bo.getLegalPersonName());
        lqw.like(StringUtils.isNotBlank(bo.getUnifiedSocialCreditCode()), Supplier::getUnifiedSocialCreditCode, bo.getUnifiedSocialCreditCode());
        lqw.between(params.get("beginEstablishmentTime") != null && params.get("endEstablishmentTime") != null,
            Supplier::getEstablishmentTime ,params.get("beginEstablishmentTime"), params.get("endEstablishmentTime"));
        lqw.like(StringUtils.isNotBlank(bo.getIndustry()), Supplier::getIndustry, bo.getIndustry());
        lqw.like(StringUtils.isNotBlank(bo.getRegistrationAddress()), Supplier::getRegistrationAddress, bo.getRegistrationAddress());
        lqw.like(StringUtils.isNotBlank(bo.getQualificationName()), Supplier::getQualificationName, bo.getQualificationName());
        lqw.eq(StringUtils.isNotBlank(bo.getQualificationFile()), Supplier::getQualificationFile, bo.getQualificationFile());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), Supplier::getRemarks, bo.getRemarks());
        return lqw;
    }

    /**
     * 新增供应商
     *
     * @param bo 供应商
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SupplierBo bo) {
        Supplier add = MapstructUtils.convert(bo, Supplier.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改供应商
     *
     * @param bo 供应商
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SupplierBo bo) {
        Supplier update = MapstructUtils.convert(bo, Supplier.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Supplier entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除供应商信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
